home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 551-575 / disk_570 / gadtoolsbox / source / source.lha / Binary.c < prev    next >
C/C++ Source or Header  |  1991-11-04  |  30KB  |  791 lines

  1. /*-- AutoRev header do NOT edit!
  2. *
  3. *   Program         :   Binary.c
  4. *   Copyright       :   © Copyright 1991 Jaba Development
  5. *   Author          :   Jan van den Baard
  6. *   Creation Date   :   12-Oct-91
  7. *   Current version :   1.00
  8. *   Translator      :   DICE v2.6
  9. *
  10. *   REVISION HISTORY
  11. *
  12. *   Date          Version         Comment
  13. *   ---------     -------         ------------------------------------------
  14. *   12-Oct-91     1.00            Binary file routines.
  15. *
  16. *-- REV_END --*/
  17.  
  18. #include "GTEd.h"
  19. #include "Protos.h"
  20.  
  21. extern struct MemoryChain       *Chain;
  22. extern UBYTE                     MainFontName[ 80 ];
  23. extern struct TextAttr           MainFont;
  24. extern struct Screen            *MainScreen;
  25. extern struct Window            *MainWindow;
  26. extern UBYTE                     MainFileName[ 512 ];
  27. extern UWORD                     MainDriPen[ NUMDRIPENS + 1 ];
  28. extern struct ColorSpec          MainColors[ 33 ];
  29. extern UBYTE                     MainScreenTitle[ 80 ];
  30. extern UBYTE                     MainWindowTitle[ 80 ];
  31. extern struct TagItem            nwTags[ 14 ];
  32. extern struct TagItem            MainSTags[ 12 ];
  33. extern struct ExtGadgetList      Gadgets;
  34. extern UWORD                     ActiveKind;
  35. extern struct Prefs              MainPrefs;
  36. extern BOOL                      Saved;
  37. extern struct NewMenu            Menus[];
  38. extern struct IntuiText         *WindowTxt;
  39. extern BOOL                      ws_InnerW, ws_InnerH, ws_ZoomF, ws_MQueue;
  40. extern BOOL                      ws_RQueue, ws_Adjust, cs_AutoScroll;
  41. extern WORD                      ws_IWidth, ws_IHeight, ws_ZLeft, ws_ZTop;
  42. extern WORD                      ws_ZWidth, ws_ZHeight, ws_MQue, ws_RQue;
  43. extern UWORD                     cs_ScreenType;
  44. extern ULONG                     WindowIDCMP, WindowFlags;
  45. extern struct ExtMenuList        ExtMenus;
  46.  
  47. /*
  48.  * Icon image data's
  49.  */
  50. UWORD IconData0[] = {
  51.   0x0000,  0x0000,  0x0000,  0x0400,  0x0000,  0x0000,  0x0000,  0x0C00,
  52.   0x0000,  0x0000,  0x0000,  0x0C00,  0x03FF,  0xFFFF,  0xFFFC,  0x0C00,
  53.   0x0300,  0x0000,  0x0000,  0x0C00,  0x0300,  0x0000,  0x0000,  0x0C00,
  54.   0x0306,  0x2A6B,  0x0000,  0x0C00,  0x0308,  0xAA4A,  0x0000,  0x0C00,
  55.   0x0308,  0x0802,  0x2000,  0x0C00,  0x0308,  0x8872,  0x2000,  0x0C00,
  56.   0x0308,  0x8842,  0x2000,  0x0C00,  0x0300,  0x8008,  0x4000,  0x0C00,
  57.   0x0307,  0x9CFB,  0x8000,  0x0C00,  0x0300,  0x0000,  0x0000,  0x0C00,
  58.   0x0300,  0x0000,  0x0000,  0x0C00,  0x0300,  0x0000,  0x0000,  0x0C00,
  59.   0x0300,  0x0000,  0x0000,  0x0C00,  0x0300,  0x0000,  0x0000,  0x0C00,
  60.   0x0200,  0x0000,  0x0000,  0x0C00,  0x0000,  0x0000,  0x0000,  0x0C00,
  61.   0x0000,  0x0000,  0x0000,  0x0C00,  0x7FFF,  0xFFFF,  0xFFFF,  0xFC00,
  62.   0xFFFF,  0xFFFF,  0xFFFF,  0xF800,  0xD555,  0x5555,  0x5555,  0x5000,
  63.   0xD555,  0x5555,  0x5555,  0x5000,  0xD400,  0x0000,  0x0003,  0x5000,
  64.   0xD400,  0x0000,  0x0007,  0x5000,  0xD40E,  0x7DF7,  0x0007,  0x5000,
  65.   0xD411,  0x5494,  0x8007,  0x5000,  0xD410,  0x1084,  0x4007,  0x5000,
  66.   0xD413,  0x10E4,  0x4007,  0x5000,  0xD411,  0x1084,  0x4007,  0x5000,
  67.   0xD411,  0x1094,  0x8007,  0x5000,  0xD40F,  0x39F7,  0x0007,  0x5000,
  68.   0xD400,  0x0000,  0x0207,  0x5000,  0xD400,  0x0000,  0x0707,  0x5000,
  69.   0xD400,  0x0000,  0x3DE7,  0x5000,  0xD400,  0x0000,  0x0707,  0x5000,
  70.   0xD400,  0x0000,  0x0207,  0x5000,  0xD400,  0x0000,  0x0007,  0x5000,
  71.   0xD5FF,  0xFFFF,  0xFFFF,  0x5000,  0xD555,  0x5555,  0x5555,  0x5000,
  72.   0xD555,  0x5555,  0x5555,  0x5000,  0x8000,  0x0000,  0x0000,  0x0000 };
  73.  
  74. UWORD IconData1[] = {
  75.   0xFFFF,  0xFFFF,  0xFFFF,  0xF800,  0xC000,  0x0000,  0x0000,  0x0000,
  76.   0xC000,  0x0000,  0x0000,  0x0000,  0xC000,  0x0000,  0x0002,  0x0000,
  77.   0xC0FF,  0xFFFF,  0xFFFE,  0x0000,  0xC0F1,  0x8208,  0xFDFE,  0x0000,
  78.   0xC0EE,  0xAB6B,  0x7DFE,  0x0000,  0xC0EF,  0xEF7B,  0xBDFE,  0x0000,
  79.   0xC0EC,  0xEF1B,  0xBDFE,  0x0000,  0xC0EE,  0xEF7B,  0xBDFE,  0x0000,
  80.   0xC0EE,  0xEF6B,  0x7DFE,  0x0000,  0xC0F0,  0xC608,  0xFDFE,  0x0000,
  81.   0xC0FF,  0xFFFF,  0xFDFE,  0x0000,  0xC0FF,  0xFFFF,  0xF8FE,  0x0000,
  82.   0xC0C0,  0x0000,  0x001E,  0x0000,  0xC0FF,  0xFFFF,  0xF8FE,  0x0000,
  83.   0xC0FF,  0xFFFF,  0xFDFE,  0x0000,  0xC0FF,  0xFFFF,  0xFFFE,  0x0000,
  84.   0xC1FF,  0xFFFF,  0xFFFE,  0x0000,  0xC000,  0x0000,  0x0000,  0x0000,
  85.   0xC000,  0x0000,  0x0000,  0x0000,  0x8000,  0x0000,  0x0000,  0x0000,
  86.   0x0000,  0x0000,  0x0000,  0x0400,  0x1555,  0x5555,  0x5555,  0x5C00,
  87.   0x1555,  0x5555,  0x5555,  0x5C00,  0x17FF,  0xFFFF,  0xFFFD,  0x5C00,
  88.   0x17FF,  0xFFFF,  0xFFF9,  0x5C00,  0x17FF,  0xFFFF,  0xFFF9,  0x5C00,
  89.   0x17F9,  0xD594,  0xFFF9,  0x5C00,  0x17F7,  0x55B5,  0xFFF9,  0x5C00,
  90.   0x17F7,  0xF7FD,  0xDFF9,  0x5C00,  0x17F7,  0x778D,  0xDFF9,  0x5C00,
  91.   0x17F7,  0x77BD,  0xDFF9,  0x5C00,  0x17FF,  0x7FF7,  0xBFF9,  0x5C00,
  92.   0x17F8,  0x6304,  0x7FF9,  0x5C00,  0x17FF,  0xFFFF,  0xFFF9,  0x5C00,
  93.   0x17FF,  0xFFFF,  0xFDF9,  0x5C00,  0x17FF,  0xFFFF,  0xFFF9,  0x5C00,
  94.   0x17FF,  0xFFFF,  0xFFF9,  0x5C00,  0x17FF,  0xFFFF,  0xFFF9,  0x5C00,
  95.   0x1600,  0x0000,  0x0001,  0x5C00,  0x1555,  0x5555,  0x5555,  0x5C00,
  96.   0x1555,  0x5555,  0x5555,  0x5C00,  0x7FFF,  0xFFFF,  0xFFFF,  0xFC00 };
  97.  
  98. struct Image Icon0 = {
  99.   0,0,54,22,2,(UWORD *)&IconData0[0],0x03,0x00,NULL };
  100. struct Image Icon1 = {
  101.   0,0,54,22,2,(UWORD *)&IconData1[0],0x03,0x00,NULL };
  102.  
  103.  
  104. struct FileRequester            *bi_Save = 0l;
  105. struct FileRequester            *bi_Load = 0l;
  106.  
  107. UBYTE                            bi_SPatt[32]   = "#?.g";
  108. UBYTE                            bi_SFile[32]  = "unnamed.g";
  109. UBYTE                            bi_SPath[256];
  110. UBYTE                            bi_LPatt[32]   = "#?.g";
  111. UBYTE                            bi_LFile[32]  = "unnamed.g";
  112. UBYTE                            bi_LPath[256];
  113.  
  114. struct TagItem                   bi_STags[] = {
  115.     ASL_Hail,                   "Save Binary As...",
  116.     ASL_Window,                 0l,
  117.     ASL_File,                   bi_SFile,
  118.     ASL_Dir,                    bi_SPath,
  119.     ASL_Pattern,                bi_SPatt,
  120.     ASL_OKText,                 "Save",
  121.     ASL_FuncFlags,              FILF_SAVE | FILF_PATGAD,
  122.     TAG_DONE };
  123.  
  124. struct TagItem                   bi_LTags[] = {
  125.     ASL_Hail,                   "Load Binary...",
  126.     ASL_Window,                 0l,
  127.     ASL_File,                   bi_LFile,
  128.     ASL_Dir,                    bi_LPath,
  129.     ASL_Pattern,                bi_LPatt,
  130.     ASL_OKText,                 "Load",
  131.     ASL_FuncFlags,              FILF_PATGAD,
  132.     TAG_DONE };
  133.  
  134. /*
  135.  * --- Write the Binary file icon.
  136.  */
  137. long WriteIcon( void )
  138. {
  139.     struct DiskObject  icon;
  140.     struct Gadget      icon_Gadget;
  141.  
  142.     setmem(( char * )&icon_Gadget, (long)sizeof( struct Gadget ), 0l );
  143.  
  144.     icon_Gadget.Width        =   54;
  145.     icon_Gadget.Height       =   22;
  146.     icon_Gadget.Flags        =   GFLG_GADGIMAGE | GFLG_GADGHIMAGE;
  147.     icon_Gadget.Activation   =   GACT_RELVERIFY | GACT_IMMEDIATE;
  148.     icon_Gadget.GadgetType   =   GTYP_BOOLGADGET;
  149.     icon_Gadget.GadgetRender =   (APTR)&Icon0;
  150.     icon_Gadget.SelectRender =   (APTR)&Icon1;
  151.  
  152.     icon.do_Magic            =   WB_DISKMAGIC;
  153.     icon.do_Version          =   WB_DISKVERSION;
  154.     icon.do_Gadget           =   icon_Gadget;
  155.     icon.do_Type             =   WBPROJECT;
  156.     icon.do_DefaultTool      =   (char *)":GadToolsBox";
  157.     icon.do_ToolTypes        =   NULL;
  158.     icon.do_CurrentX         =   NO_ICON_POSITION;
  159.     icon.do_CurrentY         =   NO_ICON_POSITION;
  160.     icon.do_DrawerData       =   NULL;
  161.     icon.do_ToolWindow       =   NULL;
  162.     icon.do_StackSize        =   8192;
  163.  
  164.     return( PutDiskObject( MainFileName, &icon ));
  165. }
  166.  
  167. /*
  168.  * --- Write all NewMenus
  169.  */
  170. void WriteNewMenus( BPTR file )
  171. {
  172.     struct ExtNewMenu   *menu, *item, *sub;
  173.     ULONG                num = 0l;
  174.  
  175.     for ( menu = ExtMenus.ml_First; menu->em_Next; menu = menu->em_Next ) {
  176.         num++;
  177.         menu->em_NumSlaves = 0;
  178.         for ( item = menu->em_Items->ml_First; item->em_Next;  item = item->em_Next ) {
  179.             menu->em_NumSlaves++;
  180.             item->em_NumSlaves = 0;
  181.             for ( sub = item->em_Items->ml_First; sub->em_Next;  sub = sub->em_Next )
  182.                 item->em_NumSlaves++;
  183.         }
  184.     }
  185.  
  186.     Write( file, (char *)&num, (long)sizeof( ULONG ));
  187.  
  188.     for ( menu = ExtMenus.ml_First; menu->em_Next; menu = menu->em_Next )
  189.         Write( file, (char *)&menu->em_NewMenu, (long)sizeof( struct ExtNewMenu ) - 14l );
  190.  
  191.     for ( menu = ExtMenus.ml_First; menu->em_Next; menu = menu->em_Next )  {
  192.         for ( item = menu->em_Items->ml_First; item->em_Next; item = item->em_Next )
  193.             Write( file, (char *)&item->em_NewMenu, (long)sizeof( struct ExtNewMenu ) - 14l );
  194.     }
  195.  
  196.     for ( menu = ExtMenus.ml_First; menu->em_Next; menu = menu->em_Next )  {
  197.         for ( item = menu->em_Items->ml_First; item->em_Next; item = item->em_Next ) {
  198.             for ( sub = item->em_Items->ml_First; sub->em_Next; sub = sub->em_Next )
  199.                 Write( file, (char *)&sub->em_NewMenu, (long)sizeof( struct ExtNewMenu ) - 14l );
  200.         }
  201.     }
  202. }
  203.  
  204. /*
  205.  * --- Write all IntuiTexts added to the window.
  206.  */
  207. void WriteITexts( BPTR file )
  208. {
  209.     struct IntuiText    *t;
  210.     ULONG                tc = 0l;
  211.  
  212.     if ( NOT( t = WindowTxt )) {
  213.         Write( file, (char *)&tc, (long)sizeof( ULONG ));
  214.         return;
  215.     }
  216.  
  217.     while ( t ) {
  218.         tc++;
  219.         t = t->NextText;
  220.     }
  221.  
  222.     Write( file, (char *)&tc, (long)sizeof( ULONG ));
  223.  
  224.     t = WindowTxt;
  225.  
  226.     while ( t ) {
  227.         Write( file, (char *)t, (long)sizeof( struct IntuiText ));
  228.         Write( file, (char *)t->IText, 80l );
  229.         t = t ->NextText;
  230.     }
  231. }
  232.  
  233. /*
  234.  * --- Write the extra data a specific kind of
  235.  * --- gadget has attached to it.
  236.  */
  237. void WriteGadgetXtra( BPTR file, struct ExtNewGadget *eng )
  238. {
  239.     struct ListViewNode *node;
  240.     ULONG                c1, c2, c3;
  241.  
  242.     switch ( eng->en_Kind ) {
  243.  
  244.         case    STRING_KIND:
  245.             c1 = 0l;
  246.  
  247.             if ( eng->en_DefString ) {
  248.                 c1 = strlen( eng->en_DefString ) + 1;
  249.                 Write( file, (char *)&c1, (long)sizeof( ULONG ));
  250.                 Write( file, eng->en_DefString, c1 );
  251.             } else
  252.                 Write( file, &c1, (long)sizeof( ULONG ));
  253.             break;
  254.  
  255.         case    LISTVIEW_KIND:
  256.             c1 = 0l;
  257.  
  258.             for ( node = eng->en_Entries.lh_Head; node->ln_Succ; node = node->ln_Succ, c1++ );
  259.  
  260.             Write( file, (char *)&c1, (long)sizeof( ULONG ));
  261.  
  262.             for ( node = eng->en_Entries.lh_Head; node->ln_Succ; node = node->ln_Succ )
  263.                 Write( file, (char *)&node->ln_UserData[0], 116l );
  264.             break;
  265.  
  266.         case    MX_KIND:
  267.         case    CYCLE_KIND:
  268.             c2 = 0;
  269.  
  270.             for ( c1 = 0l; c1 < 24l; c1++ ) {
  271.                 if ( eng->en_Labels[ c1 ] ) c2++;
  272.             }
  273.  
  274.             Write( file, (char *)&c2, (long)sizeof( ULONG ));
  275.  
  276.             if ( c2 ) {
  277.                 for ( c1 = 0l; c1 < 24l; c1++ ) {
  278.                     if ( eng->en_Labels[ c1 ] ) {
  279.                         c3 = strlen( eng->en_Labels[ c1 ] ) + 1;
  280.                         Write( file, (char *)&c1, (long)sizeof( ULONG ));
  281.                         Write( file, (char *)&c3, (long)sizeof( ULONG ));
  282.                         Write( file, (char *)eng->en_Labels[ c1 ], c3);
  283.                     }
  284.                 }
  285.             }
  286.             break;
  287.  
  288.         case    SLIDER_KIND:
  289.             c1 = 0l;
  290.  
  291.             if ( eng->en_LevelFormat ) {
  292.                 c1 = strlen( eng->en_LevelFormat ) + 1;
  293.                 Write( file, (char *)&c1, (long)sizeof( ULONG ));
  294.                 Write( file, eng->en_LevelFormat, c1 );
  295.             } else
  296.                 Write( file, (char *)&c1, (long)sizeof( ULONG ));
  297.             break;
  298.     }
  299. }
  300.  
  301. /*
  302.  * --- Write the gadgets currently in the list.
  303.  */
  304. void WriteGadgets( BPTR file )
  305. {
  306.     struct ExtNewGadget *eng;
  307.     ULONG                num = 0l;
  308.  
  309.     for ( eng = Gadgets.gl_First; eng->en_Next; eng = eng->en_Next, num++ );
  310.  
  311.     Write( file, (char *)&num, (long)sizeof( ULONG ));
  312.  
  313.     for ( eng = Gadgets.gl_First; eng->en_Next; eng = eng->en_Next ) {
  314.         Write( file, (char *)&eng->en_NewGadget, (long)( sizeof( struct ExtNewGadget ) - 16l ));
  315.         Write( file, (char *)eng->en_Tags, (long)( eng->en_NumTags * sizeof( struct TagItem )));
  316.         WriteGadgetXtra( file, eng );
  317.     }
  318. }
  319.  
  320. /*
  321.  * --- Write the Binary file.
  322.  */
  323. long WriteBinary( long req )
  324. {
  325.     struct BinHeader    head;
  326.     BPTR                file = 0l;
  327.     BOOL                ok = FALSE;
  328.     UWORD               tn = 0;
  329.  
  330.     if ( req ) {
  331.         if ( bi_Save = AllocAslRequest( ASL_FileRequest, TAG_DONE )) {
  332.             bi_STags[1].ti_Data = MainWindow;
  333.             if ( ok = AslRequest( bi_Save, bi_STags )) {
  334.  
  335.                 strcpy( MainFileName, bi_Save->rf_Dir );
  336.                 CheckDirExtension();
  337.                 strcat( MainFileName, bi_Save->rf_File );
  338.  
  339.                 CheckSuffix();
  340.  
  341.                 strcpy( bi_SPath, bi_Save->rf_Dir );
  342.                 strcpy( bi_SFile, bi_Save->rf_File );
  343.                 strcpy( bi_SPatt, bi_Save->rf_Pat );
  344.             }
  345.         }
  346.     } else ok = TRUE;
  347.  
  348.     if (( MainPrefs.pr_PrefFlags0 & PRF_WRITEICON ) == PRF_WRITEICON ) {
  349.         if ( ok ) WriteIcon();
  350.     }
  351.  
  352.     if ( ok ) {
  353.         if ( file = MyOpen( MODE_NEWFILE )) {
  354.  
  355.             SetTitle( "Saving..." );
  356.             setmem( (char *)&head, (long)sizeof( struct BinHeader ), 0l );
  357.             SetIoErr( 0l );
  358.  
  359.             head.bh_FileType        =   GT_FILETYPE;
  360.             head.bh_Version         =   GT_VERSION;
  361.             head.bh_ActiveKind      =   ActiveKind;
  362.  
  363.             strcpy( (char *)&head.bh_FontName[0], MainFontName );
  364.             CopyMem( (char *)&MainFont, (char *)&head.bh_Font, (long)sizeof( struct TextAttr ));
  365.  
  366.             strcpy( (char *)&head.bh_ScreenTitle[0], MainScreenTitle );
  367.             CopyMem( (char *)&MainSTags[0], (char *)&head.bh_ScreenTags[0], (long)( 12 * sizeof( struct TagItem )));
  368.             CopyMem( (char *)&MainColors[0], (char *)&head.bh_Colors[0], (long)( 33 * sizeof( struct ColorSpec )));
  369.             CopyMem( (char *)&MainDriPen[0], (char *)&head.bh_DriPens[0], (long)(( NUMDRIPENS + 1 ) << 1 ));
  370.  
  371.             nwTags[0].ti_Data   =   MainWindow->LeftEdge;
  372.             nwTags[1].ti_Data   =   MainWindow->TopEdge;
  373.             nwTags[2].ti_Data   =   MainWindow->Width;
  374.             nwTags[3].ti_Data   =   MainWindow->Height;
  375.  
  376.             strcpy( (char *)&head.bh_WindowTitle[0], MainWindowTitle );
  377.             CopyMem( (char *)&nwTags[0], (char *)&head.bh_WindowTags[0], (long)( 14 * sizeof( struct TagItem )));
  378.  
  379.             if ( ws_InnerW )    head.bh_Flags0 |= BHF_INNERWIDTH;
  380.             if ( ws_InnerH )    head.bh_Flags0 |= BHF_INNERHEIGHT;
  381.             if ( ws_ZoomF  )    head.bh_Flags0 |= BHF_ZOOM;
  382.             if ( ws_MQueue )    head.bh_Flags0 |= BHF_MOUSEQUEUE;
  383.             if ( ws_RQueue )    head.bh_Flags0 |= BHF_RPTQUEUE;
  384.             if ( ws_Adjust )    head.bh_Flags0 |= BHF_AUTOADJUST;
  385.  
  386.             if ( cs_AutoScroll      )      head.bh_Flags0 |= BHF_AUTOSCROLL;
  387.             if ( cs_ScreenType == 0 )      head.bh_Flags0 |= BHF_WBENCH;
  388.             else if ( cs_ScreenType == 1 ) head.bh_Flags0 |= BHF_PUBLIC;
  389.             else                           head.bh_Flags0 |= BHF_CUSTOM;
  390.  
  391.             head.bh_Zoom[ 0 ] = ws_ZLeft;
  392.             head.bh_Zoom[ 1 ] = ws_ZTop;
  393.             head.bh_Zoom[ 2 ] = ws_ZWidth;
  394.             head.bh_Zoom[ 3 ] = ws_ZHeight;
  395.  
  396.             head.bh_MouseQueue = ws_MQue;
  397.             head.bh_RptQueue   = ws_RQue;
  398.  
  399.             head.bh_IDCMP      = WindowIDCMP;
  400.             head.bh_Flags      = WindowFlags;
  401.  
  402.             Write( file, (char *)&head, (long)sizeof( struct BinHeader));
  403.             Write( file, (char *)&MainPrefs, (long)sizeof( struct MainPrefs ));
  404.             WriteGadgets( file );
  405.             WriteITexts( file );
  406.             WriteNewMenus( file );
  407.  
  408.             Close( file );
  409.  
  410.             if ( IoErr())
  411.                 MyRequest( "Oh oh...", "CONTINUE", "Write Error !" );
  412.             Saved = TRUE;
  413.         }
  414.     }
  415.  
  416.     SetWindowTitles( MainWindow, MainWindowTitle, MainScreenTitle );
  417.     RefreshWindow();
  418.     if ( bi_Save )  FreeAslRequest( bi_Save );
  419.     bi_Save = 0l;
  420.     ClearMsgPort( MainWindow->UserPort );
  421. }
  422.  
  423.  
  424. /*
  425.  * --- Read all NewMenus
  426.  */
  427. void ReadNewMenus( BPTR file )
  428. {
  429.     struct ExtNewMenu   *menu, *item, *sub;
  430.     ULONG                num = 0l, cnt;
  431.  
  432.     Read( file, (char *)&num, (long)sizeof( ULONG ));
  433.  
  434.     if ( NOT num )  return;
  435.  
  436.     for ( cnt = 0; cnt < num; cnt++ ) {
  437.         if ( menu = ( struct ExtNewMenu * )AllocItem( Chain, (long)sizeof( struct ExtNewMenu ), MEMF_PUBLIC )) {
  438.             Read( file, ( char * )&menu->em_NewMenu, (long)sizeof( struct ExtNewMenu ) - 14 );
  439.             menu->em_NewMenu.nm_Label = &menu->em_TheMenuName[0];
  440.             menu->em_NodeName         = &menu->em_TheMenuName[0];
  441.             AddTail(( struct List * )&ExtMenus, ( struct Node * )menu );
  442.         } else goto noMem;
  443.     }
  444.  
  445.     for ( menu = ExtMenus.ml_First; menu->em_Next; menu = menu->em_Next ) {
  446.         if ( menu->em_NumSlaves ) {
  447.             if ( menu->em_Items = ( struct ExtMenuList * )AllocItem( Chain, (long)sizeof( struct ExtMenuList ), MEMF_PUBLIC )) {
  448.                 NewList(( struct List * )menu->em_Items );
  449.                 for ( cnt = 0l; cnt < menu->em_NumSlaves; cnt++ ) {
  450.                     if ( item = ( struct ExtNewMenu * )AllocItem( Chain, (long)sizeof( struct ExtNewMenu ), MEMF_PUBLIC )) {
  451.                         Read( file, ( char * )&item->em_NewMenu, (long)sizeof( struct ExtNewMenu ) - 14 );
  452.                         if ( item->em_NewMenu.nm_Label != NM_BARLABEL )
  453.                             item->em_NewMenu.nm_Label = &item->em_TheMenuName[0];
  454.                         item->em_NodeName         = &item->em_TheMenuName[0];
  455.                         if ( item->em_NewMenu.nm_CommKey )
  456.                             item->em_NewMenu.nm_CommKey = &item->em_ShortCut[0];
  457.                         AddTail(( struct List * )menu->em_Items, ( struct Node * )item );
  458.                     } else goto noMem;
  459.                 }
  460.             } else goto noMem;
  461.         }
  462.     }
  463.  
  464.     for ( menu = ExtMenus.ml_First; menu->em_Next; menu = menu->em_Next ) {
  465.         for ( item = menu->em_Items->ml_First; item->em_Next; item = item->em_Next ) {
  466.             if ( item->em_NumSlaves ) {
  467.                 if ( item->em_Items = ( struct ExtMenuList * )AllocItem( Chain, (long)sizeof( struct ExtMenuList ), MEMF_PUBLIC )) {
  468.                     NewList(( struct List * )item->em_Items );
  469.                     for ( cnt = 0l; cnt < item->em_NumSlaves; cnt++ ) {
  470.                         if ( sub = ( struct ExtNewMenu * )AllocItem( Chain, (long)sizeof( struct ExtNewMenu ), MEMF_PUBLIC )) {
  471.                             Read( file, ( char * )&sub->em_NewMenu, (long)sizeof( struct ExtNewMenu ) - 14 );
  472.                             if ( sub->em_NewMenu.nm_Label != NM_BARLABEL )
  473.                                 sub->em_NewMenu.nm_Label = &sub->em_TheMenuName[0];
  474.                             sub->em_NodeName         = &sub->em_TheMenuName[0];
  475.                             if ( sub->em_NewMenu.nm_CommKey )
  476.                                 sub->em_NewMenu.nm_CommKey = &item->em_ShortCut[0];
  477.                             AddTail(( struct List * )item->em_Items, ( struct Node * )sub );
  478.                         } else goto noMem;
  479.                     }
  480.                 } else goto noMem;
  481.             } else item->em_Items = 0l;
  482.         }
  483.     }
  484.     return;
  485.  
  486.     noMem:
  487.     MyRequest( "wheeeeeeee", "OK", "Out of memory !" );
  488. }
  489.  
  490. /*
  491.  * --- Read all IntuiTexts added to the window.
  492.  */
  493. void ReadITexts( BPTR file )
  494. {
  495.     struct IntuiText    *t, *t1 = 0l;
  496.     ULONG                tc = 0l, c;
  497.  
  498.     Read( file, (char *)&tc, (long)sizeof( ULONG ));
  499.  
  500.     if ( tc ) {
  501.         for ( c = 0; c < tc; c++ ) {
  502.             if ( t = ( struct IntuiText * )AllocItem( Chain, (long)sizeof( struct IntuiText ), MEMF_PUBLIC ))
  503.                 Read( file, (char *)t, (long)sizeof( struct IntuiText ));
  504.             else goto noMem;
  505.             if ( t->IText = (UBYTE *)AllocItem( Chain, 80l, MEMF_PUBLIC ))
  506.                 Read( file, (char *)t->IText, 80l );
  507.             else goto noMem;
  508.  
  509.             if ( NOT WindowTxt ) {
  510.                 WindowTxt = t;
  511.                 t1 = WindowTxt;
  512.             } else {
  513.                 t1->NextText = t;
  514.                 t1 = t;
  515.             }
  516.         }
  517.     }
  518.  
  519.     return;
  520.  
  521.     noMem:
  522.     MyRequest( "abacadabra", "bye", "Out of memory !" );
  523.     return;
  524. }
  525.  
  526. /*
  527.  * --- Read the extra data a specific kind of
  528.  * --- gadget has attached to it.
  529.  */
  530. void ReadGadgetXtra( BPTR file, struct ExtNewGadget *eng )
  531. {
  532.     struct ListViewNode *node;
  533.     ULONG                c1, c2, c3, idx, size;
  534.  
  535.     switch ( eng->en_Kind ) {
  536.  
  537.         case    STRING_KIND:
  538.             Read( file, (char *)&c1, (long)sizeof( ULONG ));
  539.  
  540.             if ( c1 ) {
  541.                 if ( eng->en_DefString = (UBYTE *)AllocItem( Chain, c1, MEMF_PUBLIC ))
  542.                     Read( file, eng->en_DefString, c1 );
  543.                 else
  544.                     goto noMem;
  545.             }
  546.             SetTagData( eng->en_Tags, GTST_String, (Tag)eng->en_DefString );
  547.             break;
  548.  
  549.         case    LISTVIEW_KIND:
  550.             c1 = 0l;
  551.  
  552.             Read( file, (char *)&c1, (long)sizeof( ULONG ));
  553.  
  554.             for ( c2 = 0l; c2 < c1; c2++ ) {
  555.                 if ( node = MakeNode( "" )) {
  556.                     Read( file, (char *)&node->ln_UserData[0], 116l );
  557.                     AddTail( &eng->en_Entries, ( struct Node * )node );
  558.                 } else
  559.                     goto noMem;
  560.             }
  561.             SetTagData( eng->en_Tags, GTLV_Labels, (Tag)&eng->en_Entries );
  562.             break;
  563.  
  564.         case    MX_KIND:
  565.         case    CYCLE_KIND:
  566.             Read( file, (char *)&c2, (long)sizeof( ULONG ));
  567.  
  568.             if ( c2 ) {
  569.                 for ( c1 = 0l; c1 < c2; c1++ ) {
  570.                     Read( file, (char *)&idx, (long)sizeof( ULONG ));
  571.                     Read( file, (char *)&size, (long)sizeof( ULONG ));
  572.                     if ( eng->en_Labels[ idx ] = (UBYTE *)AllocItem( Chain, size, MEMF_PUBLIC ))
  573.                         Read( file, (char *)eng->en_Labels[ idx ], size);
  574.                     else
  575.                         goto noMem;
  576.                 }
  577.             }
  578.             SetTagData( eng->en_Tags, GTCY_Labels, (Tag)&eng->en_Labels[0] );
  579.             SetTagData( eng->en_Tags, GTMX_Labels, (Tag)&eng->en_Labels[0] );
  580.             break;
  581.  
  582.         case    SLIDER_KIND:
  583.             Read( file, (char *)&c1, (long)sizeof( ULONG ));
  584.  
  585.             if ( c1 ) {
  586.                 if ( eng->en_LevelFormat = (UBYTE *)AllocItem( Chain, c1, MEMF_PUBLIC ))
  587.                     Read( file, eng->en_LevelFormat, c1 );
  588.                 else
  589.                     goto noMem;
  590.             }
  591.             SetTagData( eng->en_Tags, GTSL_LevelFormat, (Tag)eng->en_LevelFormat );
  592.             break;
  593.     }
  594.     return;
  595.  
  596.     noMem:
  597.     MyRequest( "Oh boy...", "CONTINUE", "Out of memory !" );
  598.     return;
  599. }
  600.  
  601. /*
  602.  * --- Read the gadgets from the file.
  603.  */
  604. void ReadGadgets( BPTR file )
  605. {
  606.     struct ExtNewGadget *eng;
  607.     ULONG                num = 0l, cnt;
  608.  
  609.     Read( file, (char *)&num, (long)sizeof( ULONG ));
  610.  
  611.     for ( cnt = 0l; cnt < num; cnt++ ) {
  612.         if ( eng = (struct ExtNewGadget *)AllocItem( Chain, (long)sizeof( struct ExtNewGadget ), MEMF_PUBLIC )) {
  613.             Read( file, (char *)&eng->en_NewGadget, (long)( sizeof( struct ExtNewGadget ) - 16l ));
  614.  
  615.             if ( eng->en_NewGadget.ng_GadgetText )
  616.                 eng->en_NewGadget.ng_GadgetText = &eng->en_GadgetText[0];
  617.  
  618.             eng->en_Tags      = 0l;
  619.             eng->en_DefString = 0l;
  620.  
  621.             NewList( &eng->en_Entries );
  622.  
  623.             setmem( ( char *)&eng->en_Labels[0], 100l, 0l );
  624.  
  625.             eng->en_IndicatorSize = 0l;
  626.  
  627.             if ( eng->en_Tags = MakeTagList( eng->en_NumTags )) {
  628.                 Read( file, eng->en_Tags, (long)( eng->en_NumTags * sizeof( struct TagItem )));
  629.                 ReadGadgetXtra( file, eng );
  630.             } else goto noMem;
  631.         } else goto noMem;
  632.         AddTail(( struct List * )&Gadgets, ( struct Node * )eng );
  633.     }
  634.     return;
  635.  
  636.     noMem:
  637.     MyRequest( "Oh boy...", "CONTINUE", "Out of memory !" );
  638.     return;
  639. }
  640.  
  641. /*
  642.  * --- Read the Binary file.
  643.  */
  644. long ReadBinary( long req)
  645. {
  646.     struct BinHeader    head;
  647.     BPTR                file = 0l;
  648.     BOOL                ok = FALSE;
  649.  
  650.     if ( NOT Saved ) {
  651.         AlertUser( TRUE );
  652.         if ( NOT MyRequest( "huh?????","So what|Skip this","Changes made not saved !!" )) {
  653.             AlertUser( FALSE );
  654.             return TRUE;
  655.         }
  656.         AlertUser( FALSE );
  657.     }
  658.  
  659.     if ( req ) {
  660.         if ( bi_Load = AllocAslRequest( ASL_FileRequest, TAG_DONE )) {
  661.             bi_LTags[1].ti_Data = MainWindow;
  662.             if ( ok = AslRequest( bi_Load, bi_LTags )) {
  663.  
  664.                 strcpy( MainFileName, bi_Load->rf_Dir );
  665.                 CheckDirExtension();
  666.                 strcat( MainFileName, bi_Load->rf_File );
  667.  
  668.                 CheckSuffix();
  669.  
  670.                 strcpy( bi_LPath, bi_Load->rf_Dir );
  671.                 strcpy( bi_LFile, bi_Load->rf_File );
  672.                 strcpy( bi_LPatt, bi_Load->rf_Pat );
  673.             }
  674.         }
  675.     } else ok = TRUE;
  676.  
  677.     if ( ok ) {
  678.         if ( file = MyOpen( MODE_OLDFILE )) {
  679.  
  680.             if ( req ) SetTitle( "Loading..." );
  681.             SetIoErr( 0l );
  682.             DeleteAllGadgets();
  683.             DeleteTexts();
  684.             FreeNewMenus();
  685.  
  686.             Read( file, (char *)&head, (long)sizeof( struct BinHeader ));
  687.  
  688.             if ( head.bh_FileType != GT_FILETYPE ) {
  689.                 MyRequest( "Huh?", "GOTCHA", "Unknown file type !" );
  690.                 if ( req ) goto noShow;
  691.             }
  692.  
  693.             if ( ActiveKind < NUMBER_KIND )
  694.                 Menus[ 19 + ActiveKind ].nm_Flags &= ~CHECKED;
  695.             else if ( ActiveKind < SLIDER_KIND )
  696.                 Menus[ 18 + ActiveKind ].nm_Flags &= ~CHECKED;
  697.             else
  698.                 Menus[ 17 + ActiveKind ].nm_Flags &= ~CHECKED;
  699.  
  700.             ActiveKind = head.bh_ActiveKind;
  701.  
  702.             if ( ActiveKind < NUMBER_KIND )
  703.                 Menus[ 19 + ActiveKind ].nm_Flags |= CHECKED;
  704.             else if ( ActiveKind < SLIDER_KIND )
  705.                 Menus[ 18 + ActiveKind ].nm_Flags |= CHECKED;
  706.             else
  707.                 Menus[ 17 + ActiveKind ].nm_Flags |= CHECKED;
  708.  
  709.             strcpy( MainFontName, (char *)&head.bh_FontName[0] );
  710.             CopyMem( (char *)&head.bh_Font, (char *)&MainFont, (long)sizeof( struct TextAttr ));
  711.  
  712.             MainFont.ta_Name = MainFontName;
  713.  
  714.             strcpy( MainScreenTitle, (char *)&head.bh_ScreenTitle[0] );
  715.             CopyMem( (char *)&head.bh_ScreenTags[0], (char *)&MainSTags[0], (long)( 12 * sizeof( struct TagItem )));
  716.             CopyMem( (char *)&head.bh_Colors[0], (char *)&MainColors[0], (long)( 33 * sizeof( struct ColorSpec )));
  717.             CopyMem( (char *)&head.bh_DriPens[0],  (char *)&MainDriPen[0], (long)(( NUMDRIPENS + 1 ) << 1 ));
  718.  
  719.             MainSTags[6 ].ti_Data   =   (Tag)&MainScreenTitle[0];
  720.             MainSTags[7 ].ti_Data   =   (Tag)&MainDriPen[0];
  721.             MainSTags[9 ].ti_Data   =   (Tag)&MainFont;
  722.             MainSTags[10].ti_Data   =   (Tag)&MainColors[0];
  723.  
  724.             strcpy( MainWindowTitle, (char *)&head.bh_WindowTitle[0] );
  725.             CopyMem( (char *)&head.bh_WindowTags[0], (char *)&nwTags[0], (long)( 14 * sizeof( struct TagItem )));
  726.  
  727.             nwTags[6].ti_Data       =   (Tag)&MainWindowTitle[0];
  728.  
  729.             if (( head.bh_Flags0 & BHF_INNERWIDTH ) == BHF_INNERWIDTH )
  730.                 ws_InnerW = TRUE; else ws_InnerW = FALSE;
  731.             if (( head.bh_Flags0 & BHF_INNERHEIGHT ) == BHF_INNERHEIGHT )
  732.                 ws_InnerH = TRUE; else ws_InnerH = FALSE;
  733.             if (( head.bh_Flags0 & BHF_ZOOM ) == BHF_ZOOM )
  734.                 ws_ZoomF = TRUE; else ws_ZoomF = FALSE;
  735.             if (( head.bh_Flags0 & BHF_MOUSEQUEUE ) == BHF_MOUSEQUEUE )
  736.                 ws_MQueue = TRUE; else ws_MQueue = FALSE;
  737.             if (( head.bh_Flags0 & BHF_RPTQUEUE ) == BHF_RPTQUEUE )
  738.                 ws_RQueue = TRUE; else ws_RQueue = FALSE;
  739.             if (( head.bh_Flags0 & BHF_AUTOADJUST ) == BHF_AUTOADJUST )
  740.                 ws_Adjust = TRUE; else ws_Adjust = FALSE;
  741.  
  742.  
  743.             if (( head.bh_Flags0 & BHF_AUTOSCROLL ) == BHF_AUTOSCROLL )
  744.                 cs_AutoScroll = TRUE; else cs_AutoScroll = FALSE;
  745.             if (( head.bh_Flags0 & BHF_WBENCH ) == BHF_WBENCH )
  746.                 cs_ScreenType = 0;
  747.             else if (( head.bh_Flags0 & BHF_PUBLIC ) == BHF_PUBLIC )
  748.                 cs_ScreenType =1;
  749.             else
  750.                 cs_ScreenType = 2;
  751.  
  752.             ws_ZLeft   = head.bh_Zoom[ 0 ] = ws_ZLeft;
  753.             ws_ZTop    = head.bh_Zoom[ 1 ];
  754.             ws_ZWidth  = head.bh_Zoom[ 2 ];
  755.             ws_ZHeight = head.bh_Zoom[ 3 ];
  756.  
  757.             ws_MQue = head.bh_MouseQueue;
  758.             ws_RQue = head.bh_RptQueue;
  759.  
  760.             WindowIDCMP = head.bh_IDCMP;
  761.             WindowFlags = head.bh_Flags;
  762.  
  763.             Read( file, (char *)&MainPrefs, (long)sizeof( struct MainPrefs ));
  764.             ReadGadgets( file );
  765.             ReadITexts( file );
  766.             ReadNewMenus( file );
  767.  
  768.             Close( file );
  769.  
  770.             if ( IoErr()) {
  771.                 MyRequest( "Oh oh...", "CONTINUE", "Read Error !" );
  772.                 DeleteAllGadgets();
  773.                 if ( req )  goto noShow;
  774.             }
  775.             Saved = TRUE;
  776.         } else {
  777.             MyRequest( "What's up doc ?", "OH", "Could not open %s !", MainFileName );
  778.             if ( req ) goto noShow;
  779.         }
  780.     } else if ( req ) goto noShow;
  781.  
  782.     ReOpenScreen( 2l );
  783.  
  784.     noShow:
  785.     SetWindowTitles( MainWindow, MainWindowTitle, MainScreenTitle );
  786.     RefreshWindow();
  787.     if ( bi_Load )  FreeAslRequest( bi_Load );
  788.     bi_Load = 0l;
  789.     ClearMsgPort( MainWindow->UserPort );
  790. }
  791.